#!/bin/bash

# 停止脚本

APP_NAME="data-sync-task"
JAR_NAME="data-sync-task-1.0.0.jar"

echo "=========================================="
echo "停止 ${APP_NAME}..."
echo "=========================================="

# 查找进程ID
PID=$(ps -ef | grep ${JAR_NAME} | grep -v grep | awk '{print $2}')

if [ -z "$PID" ]; then
    echo "❌ 应用未运行"
    exit 1
fi

# 停止进程
echo "发现进程 PID: $PID"
kill -15 $PID

# 等待进程停止
for i in {1..10}; do
    sleep 1
    PID=$(ps -ef | grep ${JAR_NAME} | grep -v grep | awk '{print $2}')
    if [ -z "$PID" ]; then
        echo "✅ 应用已停止"
        exit 0
    fi
    echo "等待进程停止... ($i/10)"
done

# 强制停止
echo "进程未正常停止，执行强制停止..."
kill -9 $PID
echo "✅ 应用已强制停止"

echo "=========================================="
